package model

import (
	"mytest/database"
)

type User struct {
	Id   uint32 `json:"id"`
	Name string `json:"name"`
	Age  uint32 `json:"age"`
}

func (u *User) GetList(conditions string, page, pageSize int) (userList []*User, err error) {
	if page == 0 && pageSize == 0 {
		err = database.DB.Model(User{}).Where(conditions).Find(&userList).Error
		if err != nil {
			return nil, err
		}
	}
	if page > 0 && pageSize > 0 {
		var total int64
		err = database.DB.Model(User{}).Where(conditions).Count(&total).Error
		if err != nil {
			return nil, err
		}
		offsite := pageSize * (page - 1)
		err = database.DB.Model(User{}).Where(conditions).Offset(offsite).Limit(pageSize).Order("id desc").Find(&userList).Error
		if err != nil {
			return nil, err
		}
	}
	return userList, err
}
